package com.ktjy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktjy.mapper.SysUserMapper;
import com.ktjy.pojo.SysUser;
import com.ktjy.service.SysUserService;
import com.ktjy.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("sysUserService")
@Transactional
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public SysUser login(String account, String password) {
        //1.根据用户名查询数据库
        QueryWrapper qw=new QueryWrapper();
        qw.eq("account",account);
        SysUser sysUser=sysUserMapper.selectOne(qw);
        if(sysUser!=null){
            //2.判断密码
            if(password.equals(sysUser.getPassword())){
                return sysUser;
            }
        }
        return null;
    }

    @Override
    public Page<SysUser> findPage(SysUser condition, Integer pageNo, Integer pageSize) {
        Page<SysUser> page=new Page<>();
        page.setPageSize(pageSize);

        //查询总记录数
        Integer count=sysUserMapper.count(condition);
        page.setTotalCount(count);
        page.setPageNo(pageNo);
        //查询每页显示的数据
        List<SysUser> list=sysUserMapper.getPage(condition,(pageNo-1)*pageSize,pageSize);
        page.setData(list);

        return page;
    }

    @Override
    public boolean save(SysUser sysUser) {
        return sysUserMapper.insert(sysUser)>0;
    }

    @Override
    public SysUser findById(Long id) {
        return sysUserMapper.selectById(id);
    }

    @Override
    public boolean update(SysUser sysUser) {
        return sysUserMapper.updateById(sysUser)>0;
    }

    @Override
    public boolean delete(Long id) {
        return sysUserMapper.deleteById(id)>0;
    }

    @Override
    public SysUser findByAccount(String account) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("account",account);
        return sysUserMapper.selectOne(qw);
    }

}
